package com.sunday.gateway.security.authentication;

import com.sunday.common.core.enums.ErrorCodeEnum;
import com.sunday.common.core.remoting.rest.response.RestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.server.authorization.HttpStatusServerAccessDeniedHandler;
import org.springframework.security.web.server.authorization.ServerAccessDeniedHandler;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @see HttpStatusServerAccessDeniedHandler
 */
@Deprecated
@Slf4j
public class JsonServerAccessDeniedHandler implements ServerAccessDeniedHandler, JsonBaseAuthenticationHandler {

    @Override
    public Mono<Void> handle(ServerWebExchange exchange, AccessDeniedException denied) {
        RestResponse<String> restResponse = RestResponse.error(ErrorCodeEnum.A0_301, RestResponse.ShowTypeEnum.SILENT);
        log.info("拒绝访问 url : {} , error : {}", exchange.getRequest().getURI().getPath(), denied.toString());
        ServerHttpResponse response = exchange.getResponse();
        response.setStatusCode(HttpStatus.FORBIDDEN);
        return writeJson(response, restResponse);
    }

}
